﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <link href="Resource/JavaScript/layui/css/layui.css" rel="stylesheet" />
    <link href="Resource/Style/global.css" rel="stylesheet" />
    <link href="../Resource/Style/yunfilelist.css" rel="stylesheet" />
    <script src="Resource/JavaScript/jquery-1.11.3/jquery.min.js"></script>
    <script src="Resource/JavaScript/layui/layui.all.js"></script>
    <script src="Resource/JavaScript/comm/common.js"></script>

</head>
<body>
    <!--<div class="layui-layer layui-layer-page layui-layer-dir" id="layui-layer2" type="page" times="2" showtime="0" contype="object" style="z-index: 19891016; top: 149px; left: 1328px; margin-left: -15px;width:100px;">
        <div class="layui-layer-title" style="cursor: move;" id="nickname"></div>
        <div id="avatar" class="layui-layer-content" style="height: 90px;padding:0px;margin:0px;">
        </div>
    </div>-->
    <div style=" margin-left:50px; margin-top:20px;">
        <p style=" text-align:left;">
            <button type="button" class="layui-btn" id="btnUpload">
                <i class="layui-icon">&#xe67c;</i>上传文件
            </button>
            <!--<button type="button" class="layui-btn" id="btnAdd" onclick="add();">
                <i class="layui-icon layui-icon-add-circle-fine"></i>新建文件
            </button>-->
            <input type="text" id="folderName" name="folderName" style=" width:150px;display: inline;" placeholder="文件夹名称" autocomplete="off" class="layui-input">
            <button type="button" class="layui-btn" id="btnAdd" onclick="createfolder();">
                <i class="layui-icon layui-icon-add-circle-fine"></i>新建文件夹
            </button>
            <button type="button" class="layui-btn" id="btnAdd" onclick="getYunRemaining();">
                <i class="layui-icon layui-icon-auz"></i>获取剩余空间
            </button>
            <button type="button" class="layui-btn" id="btnAdd" onclick="getYunFileList();">
                <i class="layui-icon layui-icon-website"></i>获取云文档
            </button>
            <span  id="avatar"> </span>
                <span id="nickname"></span>

        </p>

        <table class="layui-table" style=" width:80%; text-align:center;">
            <thead>
                <tr>
                    <td></td>
                    <th>文件名</th>
                    <th>类型</th>
                    <!--<th>大小</th>
                    <th>创建时间</th>-->
                    <th>下载</th>
                    <th>编辑</th>
                    <th>删除</th>
                </tr>
            </thead>
            <tbody id="fileBody">
            </tbody>
        </table>
        <!--<div id="fileList" class="file-list"></div>
        <div id="phoneFileList" class="file-list-phone"></div>-->
    </div>


    <input type="hidden" id="hfAccessToken" />
    <script>

        //var isPc = true;
        //var pf = navigator.platform;
        //var bMac = (pf.indexOf("Mac") == 0);

        $(function () {
          
            getAccessToken();
        })

        function buildlist(data) {
            $("#fileBody").html("");
            var content = '';
            for (var i = 0; i < data.length; i++) {
                content += addFileInfo(data[i]);
            }
            $("#fileBody").html(content);
        }

        function addFileInfo(data) {
            var content = '';
            content += '<tr id="file_' + data.fileid + '">';

            content += '<td>' + addIcon(data.fname, data.ftype) + '</td>';
            content += '<td>' + data.fname + '</td>';
            content += '<td>' + addExt(data.fname) + '</td>';
            //content += '<td>' + data.size + '</td>';
            //content += '<td>' + data.create_date + '</td>'
            content += '<td>  <i class="layui-icon layui-icon-download-circle" style="font-size: 30px; color: #1E9FFF;" onclick="download(\'' + data.fileid + '\')"></i> </td>';
            content += '<td>  <i class="layui-icon layui-icon-edit" style="font-size: 30px; color: #1E9FFF;" onclick="edit(\'' + data.fileid + '\',\'' + data.ftype + '\')"></i> </td>';
            content += '<td>  <i class="layui-icon layui-icon-close" style="font-size: 30px; color: #1E9FFF;"  onclick="deleteFile(\'' + data.fileid + '\',\'' + data.ftype + '\')"></i> </td>'
            content += '</tr>';
            return content;
        }

        function addExt(fname) {
            var suffix = fname.substring(fname.lastIndexOf(".") + 1).toLowerCase();
            return suffix;
        }

        function addIcon(fname, ftype) {
            var src = "";
            if (ftype == "folder") {
                src = "Resource/Images/folder.png";
            } else if (ftype == "sharefile" || ftype == "file") {
                var suffix = fname.substring(fname.lastIndexOf(".") + 1).toLowerCase();
                if (suffix == "xls" || suffix == "xlsx") {
                    src = "Resource/Images/xls.png";
                } else if (suffix == "doc" || suffix == "docx") {
                    src = "Resource/Images/doc.png";
                } else if (suffix == "ppt" || suffix == "pptx") {
                    src = "Resource/Images/ppt.png";
                } else if (suffix == "xls" || suffix == "xlsx") {
                    src = "Resource/Images/xls.png";
                } else if (suffix == "txt") {
                    src = "Resource/Images/txt.png";
                } else {
                    src = "Resource/Images/other.png";
                }
            }
            return "<img src='" + src + "' />";
        }

        function download(fileid) {
            $.get("/wpsyunfile/downloadlink?fileid=" + fileid + "&accessToken=" + $("#hfAccessToken").val(), function (result) {
                if (result.Code == "0") {
                    layer.msg(result.Message);
                } else {
                    var data = JSON.parse(result.Data.downloadUrl);
                    window.open(data.url);
                }
            });
        }

         function edit(fileid,ftype) {
            if (ftype == "folder") {
                window.location.href = "/appdoc/appfilelist.html?&parentid=" + fileid;
            } else if (ftype == "sharefile" || ftype == "file") {
                $.get("/wpsyunfile/filelinkinfo?fileid=" +fileid + "&accessToken=" + $("#hfAccessToken").val(), function (result) {
                    if (result.Code == "0") {
                        layer.msg(result.Message);
                    } else {
                        var data = JSON.parse(result.Data.linkInfo);
                        window.open(data.linkurl);
                    }
                });
            }
        }

        function deleteFile(fileid)
        {
            $.get("/wpsyunfile/deletefile?fileid=" + fileid + "&accessToken=" + $("#hfAccessToken").val(), function (result) {
                    if (result.Code == "0") {
                        layer.msg(result.Message);
                    } else {
                        var data = JSON.parse(result.Data.fileDelete);
                         layer.msg(data.msg);
                    }
                });
        }

        function bindUpload() {
            layui.use('upload', function () {
                var upload = layui.upload;
                //执行实例
                var uploadInst = upload.render({
                    accept: "file",
                    elem: '#btnUpload',//绑定元素 ,
                    url: '/wpsyunfile/uploadfile?accessToken=' + $("#hfAccessToken").val(),//上传接口  ,
                    //before: function (obj) { //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                    //    if (accessToken == "") {
                    //        layer.msg("accessToken为空！");
                    //        return false;
                    //    }
                    //},
                    done: function (res) {
                        console.log(res);
                        //上传完毕回调
                        if (res.Code && res.Code == "1") {
                            layer.msg("上传成功！");
                            getYunFileList();
                            return false;
                        } else {
                              layer.msg(res.Message);
                        }
                    }
                    , error: function (e) {
                        //请求异常回调
                        layer.msg(e);
                        return false;
                    }
                });
            });
        }

        function getAccessToken() {
            var token = common.getUrlParam("token");
            var code = common.getUrlParam("code");
            $.ajax({
                url: "/authorize/token",
                type: "get",
                async: false,
                cache: false,
                dataType: "json",
                data: {
                    "token": token,
                    "code": code
                },
                success: function (d) {
                    console.log(d);
                    if (d && d.Code) {
                        if (d.Code == "1") {
                            $("#hfAccessToken").val(d.Data.access_token);
                            showUserInfo();
                            bindUpload();
                            return false;
                        } else {
                            layer.msg(d.Message);
                            return false;
                        }
                    }
                },
                error: function (err) {    //url无效，请求失败；有Exception异常，没有捕获时。
                    console.log(err);
                }
            });
        }

        function showUserInfo() {
            var token = common.getUrlParam("token");
            $.ajax({
                url: "/authorize/userInfo",
                type: "get",
                async: false,
                cache: false,
                dataType: "json",
                data: {
                    "token": token
                },
                success: function (d) {
                    console.log(d);
                    if (d && d.Code) {
                        if (d.Code == "1") {
                            var userInfo = d.Data.user;
                            $("#nickname").append("欢迎您，"+userInfo.nickname);
                            $("#avatar").append("<img style='width:30px;height:30px;' src='" + userInfo.avatar + "'/>");
                            return false;
                        } else {
                            layer.msg(d.Message);
                            return false;
                        }
                    }
                },
                error: function (err) {    //url无效，请求失败；有Exception异常，没有捕获时。
                    console.log(err);
                }
            });
        }

        function getYunFileList() {
            var accessToken = $("#hfAccessToken").val();
            if (accessToken == "") {
                layer.msg("accessToken为空！");
                return false;
            }
            $.ajax({
                url: "/wpsyunfile/filelist",
                type: "get",
                async: false,
                cache: false,
                dataType: "json",
                data: {
                    "accessToken": accessToken
                },
                success: function (d) {
                    console.log(d);
                    if (d && d.Code) {
                        if (d.Code == "1") {
                            var filelist = JSON.parse(d.Data.filelist);
                          //  initFileList(filelist.fileInfos);
                            buildlist(filelist.fileInfos);
                            return false;
                        } else {
                            layer.msg(d.Message);
                            return false;
                        }
                    }
                },
                error: function (err) {    //url无效，请求失败；有Exception异常，没有捕获时。
                    console.log(err);
                }
            });
        }

        function getYunRemaining() {
            var accessToken = $("#hfAccessToken").val();
            if (accessToken == "") {
                layer.msg("accessToken为空！");
                return false;
            }
            $.ajax({
                url: "/wpsyunfile/remaining",
                type: "get",
                async: false,
                cache: false,
                dataType: "json",
                data: {
                    "accessToken": accessToken
                },
                success: function (d) {
                    console.log(d);
                    if (d && d.Code) {
                        if (d.Code == "1") {
                            var data = JSON.parse(d.Data.remaining);
                            layer.msg("剩余空间：" + data.remaining);
                            return false;
                        } else {
                            layer.msg(d.Message);
                            return false;
                        }
                    }
                },
                error: function (err) {    //url无效，请求失败；有Exception异常，没有捕获时。
                    console.log(err);
                }
            });
        }

        function createfolder() {
            var folderName = $("#folderName").val();
            if (folderName == "") {
                layer.msg("文件夹名称为空！");
                return false;
            }
            var accessToken = $("#hfAccessToken").val();
            if (accessToken == "") {
                layer.msg("accessToken为空！");
                return false;
            }
            $.ajax({
                url: "/wpsyunfile/createfolder",
                type: "post",
                async: false,
                cache: false,
                dataType: "json",
                data: {
                    "accessToken": accessToken,
                    "name": folderName
                },
                success: function (d) {
                    console.log(d);
                    if (d && d.Code) {
                        if (d.Code == "1") {
                            //var data = JSON.parse(d.Data.remaining);
                            //layer.msg("剩余空间：" + data.remaining);
                            return false;
                        } else {
                            layer.msg(d.Message);
                            return false;
                        }
                    }
                },
                error: function (err) {    //url无效，请求失败；有Exception异常，没有捕获时。
                    console.log(err);
                }
            });
        }

    </script>
</body>
</html>